var pageIndex = 1;
var pageSize = 6;

var BASE_URL = BASE_URL;


$(function(){
	
	mui(".a-slider").slider({interval: 3000});
	
});

/**
 * 找活动首页
 */
// $(function(){
	// /*点击加载更多*/
	// mui(".mui-content").on("tap",".act-loading",function(){
		// searchActivityList();
	// });
	// mui(".a-slider").slider({interval: 3000});
	// $("#dataContainer").empty();//查询数据清空
	// searchActivityList(pageIndex,pageSize);//查询数据
	// _comm.wxShare(window.location.href,"青春上海Act+ 找活动",STATIC_URL+"/webChat/images/activity/banner.jpg","宅什么宅，出门High!","青春上海Act+ 找活动| 宅什么宅，出门High!");
// });

// /*点击加载更多*/
// mui(".mui-content").on("tap",".act-loading",function(){
	// ++pageIndex;
	// searchActivityList(pageIndex,pageSize);
// });

/**
 * 查询活动数据
 * @param pageIndex	页数
 * @param pageSize	每页数据个数
 */
function searchActivityList(pageIndex,pageSize){
	$(".act-loading").html("数据加载中...");
	 $.ajax( {  
		 url:config.searchActivityList,
		 data:{  
			 pageIndex	:	pageIndex,
			 pageSize	:	pageSize
		 },  
		 type:'GET',
		 cache:true,  
		 dataType:'json',  
		 success:function(result) {  
			/* eval()*/
			 var allPages=Math.ceil(result.data.total/pageSize);
			 
			 var activityList=eval(result.data).actLi;
			 //$("#dataContainer").html("");
	        // 渲染banner列表
			 loadActivityList(activityList);
			 
			 if(pageIndex<=allPages){		//当前也小于总数量，则显示加载更多；
				$(".act-loading").html("点击加载更多");
			 }else{
				$(".act-loading").hide();
				mui(".mui-content").off("tap",".act-loading");	/*数据加载完毕，取消“加载更多”的tap事件，终止调入接口*/
			 }
		 } 
	});
}

/**
 * 解析返回的数据
 * @param activityList
 */
function loadActivityList(activityList){
	var loadHtml="";
	if(activityList.length > 0 ){
		for(var i = 0 ; i < activityList.length; i++ ){
			
			var activity = activityList[i];//活动数据
			if(activity.shelveFlag == 0 || activity.viewActivityStatus > 5){//判断活动是否下架，已下架不显示
				continue ;
			}
			var activityPhaseStart = "";
			if(activity.activityPhases !=undefined && activity.activityPhases.length >  0 ){
				activityPhaseStart = formatterDateTime(activity.activityPhases[0].activityPhaseStart);
			}else{
				activityPhaseStart = "";
			}

			var img = activity.coverPicture?activity.coverPicture:config.resPath+"../webChat/images/lazyload.jpg";
			loadHtml+='<li class="mui-table-view-cell mui-media">'+
						'<a href="'+BASE_URL+'/webChat/activity/toActivityDetail?activityId='+activity.activityId+'">'+
				           ' <img class="mui-media-object mui-pull-left" src="'+config.replaceToHttps(img)+'" />'+
				            '<div class="mui-media-body">'+
				               '<h3 class="mui-ellipsis-2">'+activity.activityName+'</h3>'+
				               '<div class="adds">'+
				               	'<i class="icons-all icon_adds"></i>地点：<span>'+activity.activityDetailAddress+'</span>'+
				               '</div>'+
				               '<div class="adds">'+
				               	'<i class="icons-all icon_date"></i>'+activity.startEndTimeStr+
				               '</div>'+
				               '<div>'+activityDate(activity)+'</div>'+
				              '<div>';
		               //活动可见范围
		               if(activity.activityApplyScopeFlag==0 || activity.activityApplyScopeFlag==null){
		            	   //0-全网可见  1-申请范围内可见
		            	   loadHtml+='<i class="icons-all icon_eye"></i>('+activity.activityViewMembers+')<span class="line">|</span><span class="color-red">公开活动</span>';
		               }else if(activity.activityApplyScopeFlag==1){
		            	   loadHtml+='<i class="icons-all icon_eye"></i>('+activity.activityViewMembers+')<span class="line">|</span><span class="color-red">内部活动</span>';
		               }else{
		            	   loadHtml+='<i class="icons-all icon_eye"></i>('+activity.activityViewMembers+')<span class="line">|</span><span class="color-red">团员活动</span>';
		               }
		               //团员活动
		               //活动显示状态
		               if(activity.viewActivityStatus==1){
		            	   loadHtml+='<span class="s-btn s-btn-red mui-pull-right">预热中</span>';
		               }else if(activity.viewActivityStatus==2){
		            	   if(activity.activityFeeDetailsRemainMembers==0){
		            		   loadHtml+='<span class="s-btn s-btn-yellow2 mui-pull-right">名额已满</span>';
		            	   }else{
		            		   loadHtml+='<span class="s-btn s-btn-green mui-pull-right">报名中</span>';
		            	   }
		            	  
		               }else if(activity.viewActivityStatus==3){
		            	   loadHtml+='<span class="s-btn s-btn-yellow2 mui-pull-right">报名结束</span>';
		               }else if(activity.viewActivityStatus==4){
		            	   loadHtml+='<span class="s-btn s-btn-blue mui-pull-right">活动中</span>';
		               }else if(activity.viewActivityStatus==5||activity.viewActivityStatus==0){
		            	   loadHtml+='<span class="s-btn mui-pull-right">活动结束</span>';
		               }
		               
	               loadHtml+=' </div>'+
				           ' </div>'+
				        '</a>'+
				    '</li>';
		}
	}
	$("#dataContainer").append(loadHtml);//填充HTML代码
}

//活动时间
function activityDate(obj){
	   	if(obj.viewActivityStatus!=null){
	   		if(obj.viewActivityStatus==1){
	   			return "报名开始时间："+obj.applyStartTimeStr;
	   		}else if(obj.viewActivityStatus==2){
	   			return "报名结束时间："+obj.applyEndTimeStr;
	   		}else if(obj.viewActivityStatus==3){
	   			return "活动开始时间："+obj.startTimeStr;
	   		}else if(obj.viewActivityStatus==4){
	   			return "活动结束时间："+obj.endTimeStr;
	   		}else if(obj.viewActivityStatus==5 || obj.viewActivityStatus==0){
	   			if(obj.endTimeStr == null){
	   				return "";
	   			}else{
	   				return "活动结束时间："+obj.endTimeStr;
	   			}
	   		}
	   	}else{
	   		return "";
	   	}
}

/**
 * 格式化时间
 */
function formatterDateTime(time) {
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    //return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
    
    return year + "-" + month + "-" + date;
}
